import zhHans from "./zh-Hans.json"
import en from "./en.json"

const messages: any = {
  en: en,
  "zh-Hans": zhHans,
}

export const i18nConfig = {
  locale: uni.getLocale(),
  messages,
}

export const translate = (localeKey: string, opt: Record<string, any> = {}) => {
  if (!localeKey) return localeKey

  const locale = uni.getLocale()
  const message = messages[locale]

  if (Object.keys(message).includes(localeKey)) {
    const template = message[localeKey]
    // 使用 Object.keys 遍历 params 对象，替换模板中的大括号占位符
    return Object.keys(opt).reduce(
      (acc, key) => acc.replace(new RegExp(`{${key}}`, "g"), opt[key]),
      template
    )
  }
  return localeKey // 转换不了则原样输出
}
